package com.shengde.jinwawaforleader.model.myJob.instance_message;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.exceptions.HyphenateException;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.adapter.MyExpandableListAdapter;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProClassStatus;
import com.shengde.jinwawaforleader.network.protocolbean.ProLinkmenList;
import com.shengde.jinwawaforleader.utils.DecodeImageUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * 联系人列表
 * Created by Administrator on 2016/10/10 0010.
 */
public class LinkManActivity extends com.shengde.jinwawaforleader.base.BaseActivity implements ExpandableListView.OnChildClickListener {
	private static final String TAG = "LinkManActivity";
	private static final int FINSH = 1;
	private static final int CONTACT_OK = 3;
	private TextView tv_back;
	private TextView tv_title;
	private TextView fill_add;
	private ExpandableListView listView;
	private LinearLayout loading_main;
	private LinearLayout loading_no_recourse;
	private LinearLayout linear_group; //群组
	private MyExpandableListAdapter mLinkManAdapter;
	List<ProLinkmenList.DataBean> DataBean;

	private List<String> usernames;  //联系人
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (mLinkManAdapter == null) {
						mLinkManAdapter = new MyExpandableListAdapter(LinkManActivity.this, DataBean);
						listView.setAdapter(mLinkManAdapter);
						loading_main.setVisibility(View.GONE);

					} else {
						mLinkManAdapter.notifyDataSetChanged();
					}
					break;
				case 2:
					break;
				case 3:
					Toast.makeText(LinkManActivity.this, R.string.Failed_to_get_group_chat_information, Toast.LENGTH_LONG).show();
					break;

			}
		}
	};


	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.linkmen_list_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
		listView = (ExpandableListView) findViewById(R.id.lv_linkman);

		linear_group = (LinearLayout) findViewById(R.id.linear_group);
		loading_main = (LinearLayout) findViewById(R.id.loading_main);
		loading_no_recourse = (LinearLayout) findViewById(R.id.loading_no_recourse);

	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.linkmanActivity));
		listView.setGroupIndicator(null);
		showLinkmenList();
	}

	/**
	 * 获取联系人列表(通过自己服务端获取)
	 */
	private void showLinkmenList() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/friends";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/friends";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("schoolid", User.getSchoolId(this));
		params.addParameter("userid", User.getUserLoginAccount(this));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProLinkmenList pro_class_status = new Gson().fromJson(result, ProLinkmenList.class);
				if (pro_class_status != null && pro_class_status.getCode() == 0) {
					DataBean = pro_class_status.getData();
					mHandler.sendEmptyMessage(FINSH);
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}

	@Override
	public void initListener() {
		linear_group.setOnClickListener(this);
		tv_back.setOnClickListener(this);
		listView.setOnChildClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				setResult(CONTACT_OK);
				finish();
				break;
			case R.id.linear_group:
				getIntentCallBack().startActivityCommon(GroupActivity.class);
				break;
		}
	}
	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		startActivity(new Intent(LinkManActivity.this, ChatActivity.class)
				.putExtra("userId", DataBean.get(groupPosition).getData().get(childPosition).getTel())
				.putExtra(Constant.EXTRA_USER_Name,DataBean.get(groupPosition).getData().get(childPosition).getName()));
		return true;
	}

	/**
	 * 获取联系人列表(通过环信后台获取)
	 */
	private void showLinkmanList() {
		new Thread() {
			@Override
			public void run() {
				try {
					usernames = EMClient.getInstance().contactManager().getAllContactsFromServer();

					Log.i(TAG, "initData: size" + usernames.size());
					if (usernames.size() == 0 || usernames == null) {
						loading_main.setVisibility(View.GONE);
						loading_no_recourse.setVisibility(View.VISIBLE);
					}
					mHandler.sendEmptyMessage(FINSH);

				} catch (HyphenateException e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

}
